$(function() {
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }
	
   var className = '.searchInput';
	 var OutPuturl = $(".searchInput").attr('url');
    
  $(className)
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).data( "ui-autocomplete" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        source: function( request, response ) {
          $.getJSON(OutPuturl, {
            term: extractLast( request.term )
          }, response );
        },
        search: function() {
          // custom minLength
          var term = extractLast( this.value );
          if ( term.length < 3 ) {
            return false;
          }
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          // remove the current input
		  terms.pop();
		  terms.length = 0;
		  
          // add the selected item
          terms.push( ui.item.format );

		  // console.log(terms);
          // add placeholder to get the comma-and-space at the end
          this.value = terms.join( "" );
          return false;
        },position: {
			my : "left top", at: "left bottom" 
		}
    });
});